package com.example.overdue.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.example.fresh.R;
import com.example.overdue.data.OberdueData;
import com.example.overdue.util.DataUtil;

import java.util.ArrayList;
import java.util.List;

//日期的列表适配器
public class My2RecycleViewAdapter extends RecyclerView.Adapter<My2RecycleViewAdapter.MyHolder> {

    private List<OberdueData> mList;//数据源
    private String year = "";//选择的年份
    private String month = "";//选择的月份

    public My2RecycleViewAdapter(List<OberdueData> list, String year, String month) {
        mList = list;
        this.year = year;
        this.month = month;
    }

    //创建ViewHolder并返回，后续item布局里控件都是从ViewHolder中取出
    @Override
    public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        //将自定义的item布局R.layout.item_one转换为View
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_layout2, parent, false);
        //将view传递给自定义的ViewHolder
        MyHolder holder = new MyHolder(view);
        //返回这个MyHolder实体
        return holder;
    }

    //通过方法提供的ViewHolder，将数据绑定到ViewHolder中
    @Override
    public void onBindViewHolder(MyHolder holder, int position) {
        if (position == DataUtil.countDaysInMonth(year, month) - 1) {//如果是最后一个条目，隐藏掉竖线
            holder.item_shuxian.setVisibility(View.GONE);
        } else {
            holder.item_shuxian.setVisibility(View.VISIBLE);
        }

        if (position < 9) {
            holder.item_day.setText("0" + (position + 1) + "日");
        } else {
            holder.item_day.setText((position + 1) + "日");
        }
        //获取日
        String day = "";
        if (position < 9) {
            day = "0" + (position + 1);
        } else {
            day = "" + (position + 1);
        }
        List<OberdueData> oberdueDataList = new ArrayList<>();//当前年月日过期的物品数据集合
        for (int i = 0; i < mList.size(); i++) {

            if (DataUtil.showTime(mList.get(i).getTime()).contains(year + "年" + month + "月" + day+"日")) {
                oberdueDataList.add(mList.get(i));
            }
        }


        holder.item_content.setAdapter(new ImageAdapter(holder.item_content.getContext(), oberdueDataList));


    }

    //获取数据源总的条数
    @Override
    public int getItemCount() {
        return DataUtil.countDaysInMonth(year, month);
    }

    /**
     * 自定义的ViewHolder
     */
    class MyHolder extends RecyclerView.ViewHolder {

        private final ImageView item_shuxian;
        private final TextView item_day;
        private final GridView item_content;

        public MyHolder(View itemView) {
            super(itemView);
            //找id
            item_shuxian = itemView.findViewById(R.id.item_shuxian);
            item_day = itemView.findViewById(R.id.item_day);
            item_content = itemView.findViewById(R.id.item_content);
        }
    }
}

